<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class GoodsCateService extends BaseService
{
    //商品分类

    /**
     * 列表
     */
    public function getList()
    {
        $db = DB::table($this->dbGcate)->where('del', 0);
        $total = $db->count();
        $res = $db->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有分类');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $parentOne = (array) DB::table($this->dbGcate)->where('id', $v['pid'])->first();
            $pname = empty($parentOne) ? '顶层分类' : $parentOne['title'];
            $img = $v['img'] ? env('DOMAIN') . '/' . $v['img'] : '-';
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'img' => $img,
                'pname' => $pname,
                'uptime' => $v['uptime'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    /**
     * 列表
     */
    public function getClist()
    {
        $db = DB::table($this->dbGcate)->where('del', 0);
        $res = $db->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有分类');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $parentOne = (array) DB::table($this->dbGcate)->where('id', $v['pid'])->first();
            $pname = empty($parentOne) ? '顶层分类' : $parentOne['title'];
            $img = $v['img'] ? env('DOMAIN') . '/' . $v['img'] : '-';
            $arys[] = [
                'title' => $v['title'],
                'img' => $img,
                'pname' => $pname,
                'uptime' => $v['uptime'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * 选项
     */
    public function getOption()
    {
        $options = ['pid' => []];
        $options['pid'] = $this->dbQueryList($this->dbGcate,
            ['del'=>0,'pid'=>0],
            [],
            ['id'=>'id','title'=>'title']
        );
        return array('code' => 0, 'msg' => 'success', 'data' => $options);
    }

    /**
     * 一个
     * @param int $id 分类ID
     */
    public function getOne($id)
    {
        $res = (array) DB::table($this->dbGcate)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有分类');
        }
        $res = (array) $res;
        $res['imgDomain'] = env('DOMAIN') . '/';
        $res['imglink'] = $res['img'] ? $res['img'] : '';
        return array('code' => 0, 'msg' => 'success', 'data' => $res);
    }

    /**
     * 添加
     * @param array $pas
     */
    public function addOne($pas)
    {
        $res = (array) DB::table($this->dbGcate)
            ->where('del', 0)
            ->where('title', $pas['title'])
            ->first();
        if (!empty($res)) {
            return array('code' => -2, 'msg' => '分类已存在');
        }
        $arys = [
            'title' => $pas['title'],
            'img' => $pas['img'],
            'pid' => $pas['pid'],
            'addtime' => toDate(),
            'uptime' => toDate(),
        ];
        $lastid = DB::table($this->dbGcate)->insertGetId($arys);
        if (!$lastid) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 添加
     * @param int $id 分类ID
     * @param array $pas
     */
    public function modify($id, $pas)
    {
        $res = (array) DB::table($this->dbGcate)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '分类不存在');
        }
        $res = (array) DB::table($this->dbGcate)
            ->where('del', 0)
            ->where('id', '<>', $id)
            ->where('title', $pas['title'])
            ->first();
        if (!empty($res)) {
            return array('code' => -2, 'msg' => '已存在同名分类');
        }
        if (!$pas['img'] && $res['img']) {
            $img = $res['img'];
        } else {
            $img = $pas['img'];
        }
        $arys = [
            'title' => $pas['title'],
            'img' => $img,
            'pid' => $pas['pid'],
            'uptime' => toDate(),
        ];
        $up = DB::table($this->dbGcate)->where('id', $id)->update($arys);
        if (!$up) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 删除
     * @param int $id 分类ID
     */
    public function setDel($id)
    {
        $res = (array) DB::table($this->dbGcate)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '分类不存在');
        }
        $arys = [
            'del' => 1,
            'deltime' => toDate(),
        ];
        $up = DB::table($this->dbGcate)->where('id', $id)->update($arys);
        if (!$up) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }
}